Page({
  getGoodList() {
    this.setData({
      loading: true
    });
    // 
    wx.showLoading({
      title: '数据拼命加载中...',
    })
    wx.request({
      url: 'http://localhost:3000/list',
      data: {
        _page: this.data.pageCode,
      },
      success: resp => {
        this.setData({
          goodList: this.data.goodList.concat(resp.data),
          totalCount: resp.header['X-Total-Count']
        })
      },
      complete: () => {
        this.setData({
          loading: false
        })
        wx.hideLoading()
      }
    })
  },
  data: {
    bannerList: [
      '../../images/banner1.webp',
      '../../images/banner2.webp',
      '../../images/banner3.webp',
      '../../images/banner4.webp'
    ],
    menuList: [],
    goodList: [],
    // 当前分页
    pageCode: 1,
    // 数据总量
    totalCount: 0,
    finishText: '没有更多数据了',
    finished: false,
    // 节流阀
    loading: false
  },
  onLoad() {
    wx.request({
      url: 'http://localhost:3000/menu',
      success: (resp) => {
        this.setData({
          menuList: resp.data
        })
      }
    })
    this.getGoodList();
  },
  onReachBottom() {
    // 判断是否存在正在请求的数据
    if(this.data.loading) return false
    // 判断是否下一个数据
    if(this.data.goodList.length >= this.data.totalCount) {
      this.setData({
        finished: true
      })
      return false;
    }
    // 获取下一个数据， 同步跟新
    this.setData({
      pageCode: this.data.pageCode + 1
    })
    this.getGoodList();
  }
})